package com.zhijian.medical.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.format.FastDateFormat;
import cn.hutool.core.util.ObjectUtil;
import com.zhijian.medical.dao.local.PersonDeviceVerifyHistoryMapper;
import com.zhijian.medical.entity.po.PersonDeviceVerifyHistory;
import com.zhijian.medical.entity.vo.request.PersonDeviceVerifyHistoryContentVo;
import com.zhijian.medical.entity.vo.request.PersonDeviceVerifyHistorySaveOrUpdateVo;
import com.zhijian.medical.entity.vo.request.PersonDeviceVerifyHistorySearchVo;
import com.zhijian.medical.entity.vo.response.PersonDeviceVerifyHistoryVo;
import com.zhijian.medical.service.PersonDeviceVerifyHistoryService;
import com.zhijian.medical.util.JsonUtil;
import com.zhijian.medical.util.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * 设备/人员比对验证实验记录业务处理层
 *
 * @author HCG
 * @version: 1.0
 * @date 2023-07-17 19:37:29
 */
@Slf4j
@Service
public class PersonDeviceVerifyHistoryServiceImpl implements PersonDeviceVerifyHistoryService {

    @Autowired
    private PersonDeviceVerifyHistoryMapper mapper;

    @Override
    public PersonDeviceVerifyHistory selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public PersonDeviceVerifyHistoryVo selectVoById(Long id) {
        PersonDeviceVerifyHistoryVo personDeviceVerifyHistoryVo = mapper.selectVoById(id);
        if (ObjectUtil.isNull(personDeviceVerifyHistoryVo)) {
            return null;
        }
        //设备数据
        String deviceContent = personDeviceVerifyHistoryVo.getDeviceContent();
        if (StringUtils.isNotBlank(deviceContent)) {
            personDeviceVerifyHistoryVo.setDeviceContentList(JsonUtil.toArray(deviceContent, PersonDeviceVerifyHistoryContentVo.class));
        }
        return personDeviceVerifyHistoryVo;
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public Long insert(PersonDeviceVerifyHistory personDeviceVerifyHistory) {
        Date now = new Date();
        personDeviceVerifyHistory.setCreateTime(now);
        personDeviceVerifyHistory.setUpdateTime(now);
        return mapper.insert(personDeviceVerifyHistory) > 0 ? personDeviceVerifyHistory.getId() : 0L;
    }

    @Override
    public boolean save(PersonDeviceVerifyHistorySaveOrUpdateVo personDeviceVerifyHistorySaveOrUpdateVo) {
        PersonDeviceVerifyHistory personDeviceVerifyHistory = new PersonDeviceVerifyHistory();
        BeanUtils.copyProperties(personDeviceVerifyHistorySaveOrUpdateVo, personDeviceVerifyHistory);
        //设备数据
        List<PersonDeviceVerifyHistoryContentVo> deviceContentList = personDeviceVerifyHistorySaveOrUpdateVo.getDeviceContentList();
        if (CollectionUtil.isNotEmpty(deviceContentList)) {
            personDeviceVerifyHistory.setDeviceContent(JsonUtil.toString(deviceContentList));
        }
        return this.insert(personDeviceVerifyHistory) != 0;
    }

    @Override
    public boolean modify(PersonDeviceVerifyHistorySaveOrUpdateVo personDeviceVerifyHistorySaveOrUpdateVo) {
        PersonDeviceVerifyHistory personDeviceVerifyHistory = new PersonDeviceVerifyHistory();
        BeanUtils.copyProperties(personDeviceVerifyHistorySaveOrUpdateVo, personDeviceVerifyHistory);
        //设备数据
        List<PersonDeviceVerifyHistoryContentVo> deviceContentList = personDeviceVerifyHistorySaveOrUpdateVo.getDeviceContentList();
        if (CollectionUtil.isNotEmpty(deviceContentList)) {
            personDeviceVerifyHistory.setDeviceContent(JsonUtil.toString(deviceContentList));
        }
        return this.update(personDeviceVerifyHistory);
    }

    @Override
    public boolean update(PersonDeviceVerifyHistory personDeviceVerifyHistory) {
        Date now = new Date();
        personDeviceVerifyHistory.setUpdateTime(now);
        return mapper.updateByPrimaryKey(personDeviceVerifyHistory) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(PersonDeviceVerifyHistory record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public Integer count(PersonDeviceVerifyHistorySearchVo personDeviceVerifyHistorySearchVo) {
        return mapper.count(personDeviceVerifyHistorySearchVo);
    }

    @Override
    public List<PersonDeviceVerifyHistoryVo> selectList(PersonDeviceVerifyHistorySearchVo personDeviceVerifyHistorySearchVo) {
        List<PersonDeviceVerifyHistoryVo> personDeviceVerifyHistoryVos = mapper.selectList(personDeviceVerifyHistorySearchVo);
        if (CollectionUtil.isEmpty(personDeviceVerifyHistoryVos)) {
            return Collections.emptyList();
        }
        personDeviceVerifyHistoryVos.forEach(personDeviceVerifyHistoryVo -> {
            //设备数据
            String deviceContent = personDeviceVerifyHistoryVo.getDeviceContent();
            if (StringUtils.isNotBlank(deviceContent)) {
                personDeviceVerifyHistoryVo.setDeviceContentList(JsonUtil.toArray(deviceContent, PersonDeviceVerifyHistoryContentVo.class));
            }
        });
        return personDeviceVerifyHistoryVos;
    }

    @Override
    public Page<PersonDeviceVerifyHistoryVo> selectPage(PersonDeviceVerifyHistorySearchVo personDeviceVerifyHistorySearchVo) {
        //先直接查总数
        int count = count(personDeviceVerifyHistorySearchVo);
        //分页
        Page<PersonDeviceVerifyHistoryVo> page = new Page<>(true, count, personDeviceVerifyHistorySearchVo.getPageNum(), personDeviceVerifyHistorySearchVo.getPageSize());
        personDeviceVerifyHistorySearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<PersonDeviceVerifyHistoryVo> responseVos = this.selectList(personDeviceVerifyHistorySearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<PersonDeviceVerifyHistory> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

    public static final String CODE_PREFIX = "FZ-SOP-E-003-02";

    @Override
    public String selectMaxCode() {
        return mapper.selectMaxCodeByDay(DateUtil.formatDate(new Date()));
    }

    @Override
    public String createNextCode() {
        String maxCode = this.selectMaxCode();
        if (StringUtils.isBlank(maxCode)) {
            return String.format("%s-%s-1", CODE_PREFIX, DateUtil.format(new Date(), FastDateFormat.getInstance("yyyyMMdd")));
        }
        String[] maxCodeArray = maxCode.split("-");
        Integer i = Integer.valueOf(maxCodeArray[maxCodeArray.length - 1]);
        i++;
        return String.format("%s-%s-%s", CODE_PREFIX, DateUtil.format(new Date(), FastDateFormat.getInstance("yyyyMMdd")), i);
    }

}